package com.example.userserver.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.userserver.entity.CityInfo;
import com.example.userserver.enums.ReturnEnum;
import com.example.userserver.manager.CityInfoManager;
import com.example.userserver.params.PageCityInfoParams;
import com.example.userserver.params.CityInfoParams;
import com.example.userserver.utils.BeanCopyUtils;
import com.example.userserver.vo.Result;
import com.example.userserver.vo.CityInfoVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CityInfoService {
    @Autowired
    private CityInfoManager cityInfoManager;

    public PageInfo<CityInfoVO> pageCityInfo(PageCityInfoParams pageCityInfoParams){
        PageHelper.startPage(pageCityInfoParams);
        List<CityInfoVO> list = cityInfoManager.listCityInfo(pageCityInfoParams);
        PageInfo<CityInfoVO> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    public List<CityInfoVO> listCityInfo(){
        PageCityInfoParams pageCityInfoParams = new PageCityInfoParams();
        return cityInfoManager.listCityInfo(pageCityInfoParams);
    }

    public CityInfoVO getCityInfo(String id) {
        return cityInfoManager.getCityInfo(id);
    }

    public void deleteCityInfo(String id) {
        cityInfoManager.deleteCityInfo(id);
    }

    public Result updateCityInfo(CityInfoParams cityInfoParams) {
        CityInfo cityInfo = new CityInfo();
        BeanCopyUtils.copyPropertiesIgnoreNull(cityInfoParams, cityInfo);
        cityInfoManager.updateCityInfo(cityInfo);
        return new Result(null, ReturnEnum.SUCCESS);
    }

    public Result insertCityInfo(CityInfoParams cityInfoParams) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("city_name", cityInfoParams.getCityName());
        CityInfo oldCityInfo = cityInfoManager.selectCity(queryWrapper);
        if(ObjectUtil.isNotEmpty(oldCityInfo)){
            return new Result(null , ReturnEnum.CITYNAME_RE);
        }
        CityInfo cityInfo = new CityInfo();
        BeanCopyUtils.copyPropertiesIgnoreNull(cityInfoParams, cityInfo);
        cityInfoManager.insertCityInfo(cityInfo);
        return new Result(null, ReturnEnum.SUCCESS);
    }

}
